from Case.python_ui.Common.basepage import Basepage
import time,pytest

from Case.python_ui.PageLocate.admin_all_notification_locator import AdminAllNotificationLocator
from Case.python_ui.Pageobject.admin_bill_pages import AdminBill


class AdminAllNotificationPage(Basepage):


    #进入AllNotification菜单页面
    def enter_all_notification_page(self):
        time.sleep(2)
        doc = "进入Admin AllNotification菜单页面"
        for i in range(3):
            try:
                self.wait_elevisble(AdminAllNotificationLocator.approval_tab,doc)
                self.click_element(AdminAllNotificationLocator.approval_tab, doc)
                time.sleep(1)
                self.wait_elevisble(AdminAllNotificationLocator.dashboard_tab, doc)
                self.click_element(AdminAllNotificationLocator.dashboard_tab, doc)
                time.sleep(1)
                self.wait_elevisble(AdminAllNotificationLocator.bill_tab, doc)
                self.click_element(AdminAllNotificationLocator.bill_tab, doc)
                time.sleep(1)
                self.wait_elevisble(AdminAllNotificationLocator.security_management_tab, doc)
                self.click_element(AdminAllNotificationLocator.security_management_tab, doc)
                time.sleep(1)
                self.wait_elevisble(AdminAllNotificationLocator.all_notification_tab,doc)
                self.click_element(AdminAllNotificationLocator.all_notification_tab,doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue

    def refresh_page(self):
        time.sleep(2)
        doc = "页面刷新功能验"
        self.wait_elevisble(AdminAllNotificationLocator.total_count,doc)
        total = self.get_element(AdminAllNotificationLocator.total_count,doc).text
        self.wait_elevisble(AdminAllNotificationLocator.refresh_button,doc)
        self.click_element(AdminAllNotificationLocator.refresh_button,doc)
        time.sleep(2)
        total_new = self.get_element(AdminAllNotificationLocator.total_count,doc).text
        assert total == total_new

    def check_page(self):
        time.sleep(2)
        doc = "Admin All Notification列表页面检查"
        self.wait_elevisble(AdminAllNotificationLocator.all_notification,doc)
        self.click_element(AdminAllNotificationLocator.all_notification,doc)
        self.wait_elevisble(AdminAllNotificationLocator.system, doc)
        self.click_element(AdminAllNotificationLocator.system, doc)
        self.wait_elevisble(AdminAllNotificationLocator.product, doc)
        self.click_element(AdminAllNotificationLocator.product, doc)
        self.wait_elevisble(AdminAllNotificationLocator.security, doc)
        self.click_element(AdminAllNotificationLocator.security, doc)
        self.wait_elevisble(AdminAllNotificationLocator.promotion, doc)
        self.click_element(AdminAllNotificationLocator.promotion, doc)

    #Admin 未读消息显示验证
    def check_new_message(self):
        time.sleep(2)
        doc = "Admin 未读消息显示验证"
        #校验右上角图标
        #红点
        self.wait_elevisble(AdminAllNotificationLocator.new_dot_in_icon,doc)
        #new标签
        self.wait_elevisble(AdminAllNotificationLocator.message_icon,doc)
        self.click_element(AdminAllNotificationLocator.message_icon,doc)
        time.sleep(2)
        self.wait_elevisble(AdminAllNotificationLocator.new_tag_in_icon,doc)
        #校验列表红点
        self.wait_elevisble(AdminAllNotificationLocator.new_dot_in_list,doc)
        #校验菜单标签
        self.wait_elevisble(AdminAllNotificationLocator.new_tag_in_menu,doc)


    def mark_as_read_in_list(self):
        time.sleep(2)
        doc = "Admin 在列表处将消息标记为已读"
        self.wait_elevisble(AdminAllNotificationLocator.list_first_record,doc)
        self.wait_elevisble(AdminAllNotificationLocator.list_select_first_button,doc)
        self.click_element(AdminAllNotificationLocator.list_select_first_button,doc)
        time.sleep(1)
        class1 = self.get_element(AdminAllNotificationLocator.list_first_record,doc).get_attribute("class")
        if(class1 == "antd4-table-row antd4-table-row-level-0 antd4-table-row-selected"):
            self.wait_elevisble(AdminAllNotificationLocator.list_mark_as_read,doc)
            self.click_element(AdminAllNotificationLocator.list_mark_as_read,doc)
        else:
            print("Error：列表处未选中消息")
        time.sleep(3)
        class2 = self.get_element(AdminAllNotificationLocator.list_first_record, doc).get_attribute("class")
        assert class2 == "antd4-table-row antd4-table-row-level-0"

    def mark_as_read_in_bottom(self):
        time.sleep(2)
        doc = "Admin 在底部按钮处将消息标记为已读"
        self.wait_elevisble(AdminAllNotificationLocator.list_first_record,doc)
        self.wait_elevisble(AdminAllNotificationLocator.list_select_first_button,doc)
        self.click_element(AdminAllNotificationLocator.list_select_first_button,doc)
        time.sleep(1)
        class1 = self.get_element(AdminAllNotificationLocator.list_first_record,doc).get_attribute("class")
        if(class1 == "antd4-table-row antd4-table-row-level-0 antd4-table-row-selected"):
            self.wait_elevisble(AdminAllNotificationLocator.bottom_mark_as_read,doc)
            self.click_element(AdminAllNotificationLocator.bottom_mark_as_read,doc)
        else:
            print("Error：列表处未选中消息")
        time.sleep(3)
        class2 = self.get_element(AdminAllNotificationLocator.list_first_record, doc).get_attribute("class")
        assert class2 == "antd4-table-row antd4-table-row-level-0"

    def view_notification_detail(self):
        time.sleep(2)
        doc = "Admin 查看消息详情页"
        self.wait_elevisble(AdminAllNotificationLocator.first_message_title,doc)
        title = self.get_element(AdminAllNotificationLocator.first_message_title,doc).text
        self.wait_elevisble(AdminAllNotificationLocator.first_message_title_button,doc)
        self.click_element(AdminAllNotificationLocator.first_message_title_button,doc)
        time.sleep(2)
        self.wait_elevisble(AdminAllNotificationLocator.message_detail_title,doc)
        title2 = self.get_element(AdminAllNotificationLocator.message_detail_title,doc).text
        assert title == title2

    def delete_notification_in_list(self):
        time.sleep(2)
        doc = "Admin 在列表处删除通知"
        self.wait_elevisble(AdminAllNotificationLocator.total_count,doc)
        total_count = self.get_element(AdminAllNotificationLocator.total_count,doc).text
        total_count = total_count.split(" ")
        total_count = int(total_count[1],base=10)
        # print(">>>>>>>>>>>>>>>total_count:",total_count)
        self.wait_elevisble(AdminAllNotificationLocator.list_delete_button, doc)
        self.click_element(AdminAllNotificationLocator.list_delete_button, doc)
        self.wait_elevisble(AdminAllNotificationLocator.delete_confirm_button,doc)
        self.click_element(AdminAllNotificationLocator.delete_confirm_button,doc)
        time.sleep(2)
        self.wait_elevisble(AdminAllNotificationLocator.total_count, doc)
        result = self.get_element(AdminAllNotificationLocator.total_count, doc).text
        result = result.split(" ")
        result = int(result[1],base=10)
        # print(">>>>>>>>>>>>>>>result:", result)
        # print(">>>>>>>>>>>>>>>result+1:", result+1)
        assert total_count == result + 1

    def delete_notification_in_bottom(self):
        time.sleep(2)
        doc = "Admin 在底部按钮处删除通知"
        self.wait_elevisble(AdminAllNotificationLocator.total_count, doc)
        total_count = self.get_element(AdminAllNotificationLocator.total_count, doc).text
        total_count = total_count.split(" ")
        total_count = int(total_count[1], base=10)
        self.wait_elevisble(AdminAllNotificationLocator.list_first_record, doc)
        self.wait_elevisble(AdminAllNotificationLocator.list_select_first_button, doc)
        self.click_element(AdminAllNotificationLocator.list_select_first_button, doc)
        time.sleep(1)
        class1 = self.get_element(AdminAllNotificationLocator.list_first_record, doc).get_attribute("class")
        if (class1 == "antd4-table-row antd4-table-row-level-0 antd4-table-row-selected"):
            self.wait_elevisble(AdminAllNotificationLocator.bottom_delete, doc)
            self.click_element(AdminAllNotificationLocator.bottom_delete, doc)
            self.wait_elevisble(AdminAllNotificationLocator.delete_confirm_button, doc)
            self.click_element(AdminAllNotificationLocator.delete_confirm_button, doc)
            AdminBill.wait_web_page_rotate(self,10)  # 页面转圈
            self.wait_elevisble(AdminAllNotificationLocator.total_count, doc)
            result = self.get_element(AdminAllNotificationLocator.total_count, doc).text
            result = result.split(" ")
            result = int(result[1], base=10)
            assert total_count == result + 1
        else:
            print("Error：列表处未选中消息")
            assert 0 == 1


    def flip_notification_detail_page(self):
        time.sleep(4)
        doc = "Admin 站内信查看页面优化"
        self.wait_elevisble(AdminAllNotificationLocator.total_count,doc)
        result = self.get_element(AdminAllNotificationLocator.total_count, doc).text
        result = result.split(" ")
        result = int(result[1], base=10)
        if(result > 0):
            self.wait_elevisble(AdminAllNotificationLocator.first_message_title_button, doc)
            self.click_element(AdminAllNotificationLocator.first_message_title_button, doc)
            time.sleep(2)
            self.wait_elevisble(AdminAllNotificationLocator.previous_button,doc)
            is_disable = self.get_element(AdminAllNotificationLocator.previous_button,doc).get_attribute("disabled")
            assert is_disable == "true"
            self.wait_elevisble(AdminAllNotificationLocator.close_message_detail, doc)
            self.click_element(AdminAllNotificationLocator.close_message_detail, doc)

        if(result > 2):
            self.wait_elevisble(AdminAllNotificationLocator.second_message_title_button, doc)
            time.sleep(5)
            self.click_element(AdminAllNotificationLocator.second_message_title_button, doc)
            time.sleep(2)
            self.wait_elevisble(AdminAllNotificationLocator.previous_button, doc)
            is_disable = self.get_element(AdminAllNotificationLocator.previous_button, doc).get_attribute("disabled")
            print('>>>>>>>>>>>>>>>>>>>>isdisable1:',is_disable)
            assert is_disable == None
            self.wait_elevisble(AdminAllNotificationLocator.close_message_detail, doc)
            is_disable = self.get_element(AdminAllNotificationLocator.previous_button, doc).get_attribute("disabled")
            assert is_disable == None
            #self.click_element(AdminAllNotificationLocator.previous_button, doc)

